package com.nika.usercenter.controller.Test;


import com.nika.usercenter.mapper.TeamMapper;
import com.nika.usercenter.model.domain.Team;
import com.nika.usercenter.model.domain.User;
import com.nika.usercenter.service.TeamService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "测试模块")
@RestController
@RequestMapping("/test")
public class CookieTestController {


    @Resource
    private TeamService teamService;

    @GetMapping("/cookie/read")
    public Map cookieReadByRequest(HttpServletRequest request) {
        Map returnData = new HashMap();
        returnData.put("result", "this is song list");
        returnData.put("author", "wht");
        Cookie[] cookies = request.getCookies();
        returnData.put("cookies", cookies);
        return returnData;
    }

    @GetMapping("/teams")
    public List<Team> getTeam(){
       return teamService.list();
    }

    @GetMapping("/cookie/readByjSessionId")
    public Map cookieReadByJsessionId(@CookieValue("JSESSIONID") String jSessionId) {
        Map returnData = new HashMap();
        returnData.put("result", "this is song list");
        returnData.put("author", "wht");
        returnData.put("JSESSIONID", jSessionId);
        return returnData;
    }
    @GetMapping("/cookie/write")
    public Map writeCookie(HttpServletResponse response) {
        Map returnData = new HashMap();
        returnData.put("result", "this is song list");

        Cookie cookie = new Cookie("sessionId","test");
        // 设置的是 cookie 的域名，就是会在哪个域名下生成 cookie 值
        cookie.setDomain("127.0.0.1");
        // 是 cookie 的路径，一般就是写到 / ，不会写其他路径的
        cookie.setPath("/");
        // 设置cookie 的最大存活时间，-1 代表随浏览器的有效期，也就是浏览器关闭掉，这个 cookie 就失效了。
        cookie.setMaxAge(3600);
        // 设置是否只能服务器修改，浏览器端不能修改，安全有保障
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        returnData.put("message", "add cookie successful");
        return returnData;
    }
}
